<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 用户管理
 */
class OrderController extends BaseController
{
    /**
     * 用户列表
     * @return [type] [description]
     */
    public function index($key="")
    {	
		$where = array();
        if($key == ""){
            $model = M('orders');  
        }else{
            //$where['username'] = trim($key);
            //$model = M('jubao')->where($where); 
        } 

        
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('member', $member);
        $this->assign('page',$show);
        $this->display();     
    }

    /**
     * 更新管理员信息
     * @param  [type] $id [管理员ID]
     * @return [type]     [description]
     */
    public function update()
    {
        //默认显示添加表单
        if (!IS_POST) {

            $id = I('id');

			$info = M('Ticket')->find($id);
			$this->assign('info',$info);
			$list = M('Ticketreply')->where(array('tid'=>$id))->order('id asc')->select();
			$this->assign('list',$list);

            $this->fid = session('adminId');
            $this->display();
        }
        if (IS_POST) { 
			$id = I('post.id');
			$tid = $id;
			if(!trim(I('post.desc'))){
				$this->error('回复不能为空');exit;
			}
			$nowtime = time();
            $uid = session('adminId');

			$data = array();
			$data['status'] = "1";
			$tid = M("Ticket")->where(array("id"=>$id))->save($data);

			$data = array();
			$data['desc'] = trim(I('post.desc'));
			$data['addtime'] = date('Y-m-d H:i:s',$nowtime);
			$data['userid'] = $uid;
			$data['tid'] = $tid;
			$trid = M("Ticketreply")->add($data);

            $this->success("回复成功", U('ticket/index'));

        }
    }

    /**
     * 删除管理员
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
    	if(C('SUPER_ADMIN_ID') == $id) $this->error("超级管理员不可禁用!");
        $model = M('admin');
        //查询status字段值
        $result = $model->find($id);
        //更新字段
        $data['id']=$id;
        if($result['status'] == 1){
        	$data['status']=0;
        }
        if($result['status'] == 0){
        	$data['status']=1;
        }
        if($model->save($data)){
            $this->success("状态更新成功", U('admin/index'));
        }else{
            $this->error("状态更新失败");
        }
    }

    /**
     * 批量删除
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function alldel($id) {
       
  
            if (empty($id)) {
                $this->error("不能提交空值!");
            }

            $member = M("Admin");
            $result = $member->where(array('id'=>$id))->find();

            $post = M('List'); 
            $hobby = $post->where(array('fid'=>$id))->field('id')->select();
            
            foreach ($hobby as $k => $val) {

                $map['id'] = $val['id'];
                unlink('./Public/appipa/'.$val['id'].'.plist');
                unlink('./Public/png/'.$val['id'].'.png');
                /**
                if ($result['uptype'] == 1) {
                    // 初始化签权对象
                    $auth = new \Qiniu\Auth(trim($result['accesskey']), trim($result['secretkey']));

                    //初始化BucketManager
                    $bucketMgr = new \Qiniu\Storage\BucketManager($auth);
                    $err = $bucketMgr->delete(trim($result['bucket']), trim($result['qnname']));
                }
                **/

                if ($result['uptype'] == 2) {
                    $uptxt = $post->where(array('id'=>$val['id']))->getFiled('uptxt');
                    unlink($uptxt);
                }
                $res = $post->where($map)->delete();
            }
            
            $memenum = $member->where("id=".$id)->delete();
            if($memenum){
                
                    //unlink('./Public/uploads/'.$info['uptxt']);    
                $this->success("删除成功", U('admin/index'));
            }else{
                $this->error("删除失败");
            }
            
  
    }

    
}
